package com.bright.cwduijie.duijie.service.impl;

import com.bright.cwduijie.duijie.enums.DataApi;
import com.bright.cwduijie.duijie.enums.OpType;
import com.bright.cwduijie.duijie.enums.SJJHBZ;
import com.bright.cwduijie.duijie.pojo.bo.ApiDataCollection;
import com.bright.cwduijie.duijie.pojo.po.Contract;
import com.bright.cwduijie.duijie.pojo.query.DuijieQuery;
import com.bright.cwduijie.duijie.repository.ContractRepository;
import com.bright.cwduijie.duijie.service.AbstractPerThousandApiService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;

/**
 * @Author hxj
 * @Date 2024/7/29 14:43
 * @Description
 */

@Component
@RequiredArgsConstructor
@Slf4j
public class ApiContractServiceImpl extends AbstractPerThousandApiService<Contract> {

    private final ContractRepository contractRepository;

    @Override
    public JpaSpecificationExecutor getJpaSpecificationExecutor() {
        return contractRepository;
    }

    @Override
    public DataApi getDataApi() {
        return DataApi.CONTRACT;
    }

    @Override
    public Function<Contract, Integer> getIdFunc() {
        return Contract::getID;
    }

    @Override
    protected void getData(ApiDataCollection apiDataCollection, DuijieQuery duijieQuery) {
//        List<Contract> contracts = contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.WAITING.getValue(), OpType.ADD_OR_EDIT.getValue());
//        contracts.addAll(contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.ERROR.getValue(), OpType.ADD_OR_EDIT.getValue()));
        apiDataCollection.setContracts(getNeedDuijieData(duijieQuery));
    }

    @Override
    public Function<Contract, String> getDataKeyFunc() {
        return Contract::getDATA_KEY;
    }

    @Override
    public Function<ApiDataCollection, List<Contract>> getDataFunc() {
        return ApiDataCollection::getContracts;
    }

//    @Override
//    protected List<Contract> getNeedDeleteData(DuijieQuery duijieQuery) {
//        List<Contract> contracts = contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.WAITING.getValue(), OpType.DEL.getValue());
//        contracts.addAll(contractRepository.getAllByTYSHXYDMAndSjjhbzAndOpType(duijieQuery.getCreditCode(), SJJHBZ.ERROR.getValue(), OpType.DEL.getValue()));
//        return contracts;
//    }

    @Override
    protected DataApi getDeleteDataApi() {
        return DataApi.DELETE_CONTRACT;
    }
}
